Merge branch 'master' of https://xp-dev.com/git/RRRRHHHH_Code
[RRRRHHHH_Code] / ruralHouses client / src / gui / ModifyOfferGUI.java
1 package gui;
2
3 import java.awt.Color;
4 import java.awt.Dimension;
5 import java.awt.Rectangle;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.FocusEvent;
9 import java.awt.event.FocusListener;
10 import java.awt.event.ItemEvent;
11 import java.awt.event.ItemListener;
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14 import java.rmi.Naming;
15 import java.rmi.RemoteException;
16 import java.sql.Date;
17 import java.text.DateFormat;
18 import java.util.Calendar;
19 import java.util.Locale;
20 import java.util.Vector;
21
22 import javax.swing.JButton;
23 import javax.swing.JComboBox;
24 import javax.swing.JFrame;
25 import javax.swing.JLabel;
26 import javax.swing.JTextField;
27
28 import com.toedter.calendar.JCalendar;
29 import common.HouseInterface;
30 import common.OfferInterface;
31
32 import configuration.___IntNames;
33 import domain.Offer;
34 import domain.Owner;
35 import domain.RuralHouse;
36 import exceptions.BadDates;
37
38 public class ModifyOfferGUI extends JFrame {
39
40         private static final long serialVersionUID = 1L;
41
42         private JComboBox<RuralHouse> jComboBox1;
43         private JLabel jLabel1 = new JLabel();
44         private JLabel jLabel2 = new JLabel();
45         private JTextField jTextField1 = new JTextField();
46         private JLabel jLabel3 = new JLabel();
47         private JTextField jTextField2 = new JTextField();
48         private JLabel jLabel4 = new JLabel();
49         private JTextField jTextField3 = new JTextField();
50         private JButton jButton1 = new JButton();
51         // Code for JCalendar
52         private JCalendar jCalendar1 = new JCalendar();
53         private JCalendar jCalendar2 = new JCalendar();
54         private Calendar calendarInicio = null;
55         private Calendar calendarFin = null;
56         private JButton jButton2 = new JButton();
57         private JLabel jLabel5 = new JLabel();
58         private final JLabel jLabel1_o = new JLabel();
59         private JComboBox<Offer> comboBox_o;
60         private Vector<RuralHouse> Hlist = null;
61         private OfferInterface  om = null;
62         private HouseInterface hm = null;
63
64
65         public ModifyOfferGUI(Owner o) {
66                 try {
67                         jbInit(o);
68                 } catch (Exception e) {
69                         e.printStackTrace();
70                 }
71         }
72
73         private void jbInit(Owner o) throws Exception {
74                 this.getContentPane().setLayout(null);
75                 this.setSize(new Dimension(513, 433));
76                 this.setTitle("Set availability");
77
78
79                 try {
80                         om = (OfferInterface) Naming
81                                         .lookup(___IntNames.OfferManager);
82                         hm = (HouseInterface) Naming
83                                         .lookup(___IntNames.HouseManager);
84                 } catch (Exception e1) {
85                         System.out.println("Error accessing remote authentication: "
86                                         + e1.toString());
87                 }
88                 try {
89                         Hlist = hm.getHouses(o, null, null, 0, 0, 0, 0, 0);
90                 } catch (RemoteException e1) {
91                         e1.printStackTrace();
92                 }               
93                 jComboBox1 = new JComboBox<RuralHouse>(Hlist);
94
95                 comboBox_o = new JComboBox<Offer>(
96                                 ((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers());
97                 DateFormat dateformat1 = DateFormat.getDateInstance(1,
98                                 jCalendar1.getLocale());
99                 if (!((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers()
100                                 .isEmpty()) {
101                         jTextField1.setText(dateformat1.format(((Offer) comboBox_o
102                                         .getSelectedItem()).getFirstDay()));
103                         jTextField2.setText(dateformat1.format(((Offer) comboBox_o
104                                         .getSelectedItem()).getLastDay()));             
105                         jLabel4.setText(Float.toString(((Offer) comboBox_o
106                                         .getSelectedItem()).getPrice()));
107                         jTextField3.setText(Float.toString(((Offer) comboBox_o
108                                         .getSelectedItem()).getPrice()));
109                 } else {
110                         jLabel5.setText("There are no offers for the selected rural house");
111                         jCalendar1.setEnabled(false);
112                         jCalendar2.setEnabled(false);
113                         jButton1.setEnabled(false);
114                         comboBox_o.setEnabled(false);
115                         jTextField3.setEnabled(false);
116                 }
117                 jComboBox1.setBounds(new Rectangle(115, 12, 115, 20));
118                 jLabel1.setText("List of houses:");
119                 jLabel1.setBounds(new Rectangle(25, 12, 95, 20));
120                 jLabel2.setText("First day :");
121                 jLabel2.setBounds(new Rectangle(25, 75, 85, 25));
122                 jTextField1.setBounds(new Rectangle(25, 265, 220, 25));
123                 jTextField1.setEditable(false);
124                 jLabel3.setText("Last day :");
125                 jLabel3.setBounds(new Rectangle(260, 75, 75, 25));
126                 jTextField2.setBounds(new Rectangle(260, 265, 220, 25));
127                 jTextField2.setEditable(false);
128                 jLabel4.setText("Price:");
129                 jLabel4.setBounds(new Rectangle(260, 30, 75, 20));
130                 jTextField3.setBounds(new Rectangle(350, 30, 115, 20));
131                 jTextField3.setText("0");
132                 jButton1.setText("Accept");
133                 jButton1.setBounds(new Rectangle(100, 360, 130, 30));
134                 jTextField3.addFocusListener(new FocusListener() {
135                         public void focusGained(FocusEvent e) {
136                         }
137
138                         public void focusLost(FocusEvent e) {
139                                 jTextField3_focusLost();
140                         }
141                 });
142
143                 jComboBox1.addItemListener(new ItemListener() {
144
145                         @Override
146                         public void itemStateChanged(ItemEvent arg0) {
147
148                                 Vector<Offer> vo= null;
149                                 try {
150                                         vo = om.getRuralHouseOffers((RuralHouse) jComboBox1.getSelectedItem());
151                                 } catch (RemoteException e) {
152                                         // TODO Auto-generated catch block
153                                         e.printStackTrace();
154                                 }
155                                 comboBox_o.removeAllItems();
156                                 if (!vo.isEmpty()) {
157                                         jCalendar1.setEnabled(true);
158                                         jCalendar2.setEnabled(true);
159                                         jButton1.setEnabled(true);
160                                         comboBox_o.setEnabled(true);
161                                         jTextField3.setEnabled(true);
162                                         jLabel5.setText("");
163                                         for (Offer of : vo) {
164                                                 comboBox_o.addItem(of);
165                                         }
166                                 } else {
167                                         jLabel5.setText("There are no offers for the selected rural house");
168                                         jCalendar1.setEnabled(false);
169                                         jCalendar2.setEnabled(false);
170                                         jButton1.setEnabled(false);
171                                         comboBox_o.setEnabled(false);
172                                         jTextField3.setEnabled(false);
173                                         
174                                 }
175                         }
176
177                 });
178
179                 comboBox_o.addItemListener(new ItemListener() {
180
181                         @Override
182                         public void itemStateChanged(ItemEvent arg0) {
183                                 if (arg0.getStateChange() == ItemEvent.SELECTED) {
184                                         Offer of = (Offer) comboBox_o.getSelectedItem();
185                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
186                                                         jCalendar1.getLocale());
187                                         jTextField1.setText(dateformat1.format(of.getFirstDay()));
188                                         jTextField2.setText(dateformat1.format(of.getLastDay()));
189                                 }
190                         }
191
192                 });
193
194                 jButton1.addActionListener(new ActionListener() {
195                         public void actionPerformed(ActionEvent e) {
196                                 jButton1_actionPerformed(e);
197                         }
198                 });
199                 jButton2.setText("Cancel");
200                 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
201                 jButton2.addActionListener(new ActionListener() {
202                         public void actionPerformed(ActionEvent e) {
203                                 jButton2_actionPerformed(e);
204                         }
205                 });
206                 jLabel5.setBounds(new Rectangle(100, 320, 300, 20));
207                 jLabel5.setForeground(Color.red);
208                 jLabel5.setSize(new Dimension(305, 20));
209                 jCalendar1.setBounds(new Rectangle(25, 100, 220, 165));
210                 jCalendar2.setBounds(new Rectangle(260, 100, 220, 165));
211
212                 // Code for JCalendar
213                 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
214                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
215                                 if (propertychangeevent.getPropertyName().equals("locale")) {
216                                         jCalendar1.setLocale((Locale) propertychangeevent
217                                                         .getNewValue());
218                                         DateFormat dateformat = DateFormat.getDateInstance(1,
219                                                         jCalendar1.getLocale());
220                                         jTextField1.setText(dateformat.format(calendarInicio
221                                                         .getTime()));
222                                 } else if (propertychangeevent.getPropertyName().equals(
223                                                 "calendar")) {
224                                         calendarInicio = (Calendar) propertychangeevent
225                                                         .getNewValue();
226                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
227                                                         jCalendar1.getLocale());
228                                         jTextField1.setText(dateformat1.format(calendarInicio
229                                                         .getTime()));
230                                         jCalendar1.setCalendar(calendarInicio);
231                                 }
232                         }
233                 });
234
235                 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener() {
236                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
237                                 if (propertychangeevent.getPropertyName().equals("locale")) {
238                                         jCalendar2.setLocale((Locale) propertychangeevent
239                                                         .getNewValue());
240                                         DateFormat dateformat = DateFormat.getDateInstance(1,
241                                                         jCalendar2.getLocale());
242                                         jTextField2.setText(dateformat.format(calendarFin.getTime()));
243                                 } else if (propertychangeevent.getPropertyName().equals(
244                                                 "calendar")) {
245                                         calendarFin = (Calendar) propertychangeevent.getNewValue();
246                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
247                                                         jCalendar2.getLocale());
248                                         jTextField2.setText(dateformat1.format(calendarFin
249                                                         .getTime()));
250                                         jCalendar2.setCalendar(calendarFin);
251                                 }
252                         }
253                 });
254
255                 this.getContentPane().add(jCalendar2, null);
256                 this.getContentPane().add(jCalendar1, null);
257                 this.getContentPane().add(jLabel5, null);
258                 this.getContentPane().add(jButton2, null);
259                 this.getContentPane().add(jButton1, null);
260                 this.getContentPane().add(jTextField3, null);
261                 this.getContentPane().add(jLabel4, null);
262                 this.getContentPane().add(jTextField2, null);
263                 this.getContentPane().add(jLabel3, null);
264                 this.getContentPane().add(jTextField1, null);
265                 this.getContentPane().add(jLabel2, null);
266                 this.getContentPane().add(jLabel1, null);
267                 this.getContentPane().add(jComboBox1, null);
268                 jLabel1_o.setText("List of offers:");
269                 jLabel1_o.setBounds(new Rectangle(25, 30, 95, 20));
270                 jLabel1_o.setBounds(25, 44, 95, 20);
271
272                 getContentPane().add(jLabel1_o);
273                 comboBox_o.setBounds(new Rectangle(115, 30, 115, 20));
274                 comboBox_o.setBounds(115, 44, 115, 20);
275
276                 getContentPane().add(comboBox_o);
277         }
278
279         private void jButton1_actionPerformed(ActionEvent e) {
280                 RuralHouse ruralHouse = ((RuralHouse) jComboBox1.getSelectedItem());
281                 Date firstDay = new Date(jCalendar1.getCalendar().getTime().getTime());
282                 // Remove the hour:minute:second:ms from the date
283                 firstDay = Date.valueOf(firstDay.toString());
284                 Date lastDay = new Date(jCalendar2.getCalendar().getTime().getTime());
285                 // Remove the hour:minute:second:ms from the date
286                 lastDay = Date.valueOf(lastDay.toString());
287                 
288
289                 try {
290
291                         // It could be to trigger an exception if the introduced string is
292                         // not a number
293                         float price = Float.parseFloat(jTextField3.getText());
294
295                         // Obtain the business logic from a StartWindow class (local or
296                         // remote)
297                         OfferInterface offerM = null;
298                         try {
299                                 offerM = (OfferInterface) Naming
300                                                 .lookup(___IntNames.OfferManager);
301                         } catch (Exception e1) {
302                                 System.out.println("Error accessing remote authentication: "
303                                                 + e1.toString());
304                         }
305                         
306                         offerM.deleteOffer(ruralHouse, (Offer) comboBox_o.getSelectedItem());
307                         offerM.createOffer(ruralHouse, firstDay, lastDay, price);
308
309                         jLabel5.setText("Offer modified");
310
311                 } catch (java.lang.NumberFormatException e1) {
312                         jLabel5.setText(jTextField3.getText() + " is not a valid price");
313                 } catch (BadDates e1) {
314                         jLabel5.setText("Last day is before first day in the offer");
315                 } catch (Exception e1) {
316                         e1.printStackTrace();
317                 }
318         }
319
320         private void jButton2_actionPerformed(ActionEvent e) {
321                 this.setVisible(false);
322         }
323
324         private void jTextField3_focusLost() {
325                 try {
326                         new Integer(jTextField3.getText());
327                         jLabel5.setText("");
328                 } catch (NumberFormatException ex) {
329                         jLabel5.setText("Error: Please introduce a number");
330                 }
331         }
332 }